// Store DMSF controls
var dmsfButtons = $('#dmsf_buttons')[0].outerHTML;
var browserInfo = $('#browser_info').text();
var tag = $('#dmsf_tag');
var dmsfTag;
var browser = $("#browser");

if((tag != null) && tag[0] != null) {

    dmsfTag = tag[0].outerHTML
}

// Destroy the original dataTable
browser.dataTable().fnDestroy();

// Add rows
$('#<%= params[:row_id] %>').after('<%= escape_javascript(render(:partial => 'dmsf/dmsf_rows')) %>');
hideOnLoad();
$('#dmsf_uploader').hide();
$('#dmsf_uploader_header').hide();
$("#dmsf_ajax_loading").show();

// Reinitialize the dataTable
<% title = DmsfFolder.get_column_position('title') %>
<% position = DmsfFolder.get_column_position('position') %>
<% commands = DmsfFolder.get_column_position('commands') %>
<% position = DmsfFolder.get_column_position('position') %>
<% version = DmsfFolder.get_column_position('version') %>
<% size_calculated = DmsfFolder.get_column_position('size_calculated') %>
<% modified_calculated = DmsfFolder.get_column_position('modified_calculated') %>
<% version_calculated = DmsfFolder.get_column_position('version_calculated') %>
<% size = DmsfFolder.get_column_position('size') %>
<% modified = DmsfFolder.get_column_position('modified') %>

browser.dataTable({
  orderClasses: false,
  responsive: {
      details: false
  },
  language: {
    url: "<%= plugin_asset_path(:redmine_dmsf, 'javascripts', json_url) %>"
  },
  autoWidth: false,
  paginate: false,
  <% if title %>
    order: [[<%= title %>, "asc"]],
  <% end %>
  sortingFixed: [[ <%= position %>, "asc"]],
  columnDefs: [
      { searchable: false, targets: [0, <%= commands %>, <%= position %>, <%= size_calculated %>, <%= modified_calculated %>, <%= version_calculated %>] },
      { sortable: false, targets: [0, <%= commands %>] }
      <% if size %>
        ,{ iDataSort: <%= size_calculated %>, targets: [ <%= size %> ] }
      <% end %>
      <% if modified %>
        ,{ iDataSort: <%= modified_calculated %>, targets: [ <%= modified %> ] }
      <% end %>
      <% if version %>
        ,{ iDataSort: <%= version_calculated %>, targets: [ <%= version %> ] }
      <% end %>
  ],
  "fnInitComplete": function() {
      var wrapper = $("#browser_wrapper div.fg-toolbar")[0];
      $(dmsfButtons).prependTo(wrapper);
      if($(dmsfTag) != null) {
        $(dmsfTag).prependTo(wrapper);
      }
      $("#browser").show();
      $('#dmsf_uploader').show();
      $('#dmsf_uploader_header').show();
      $("#dmsf_ajax_loading").hide();
  },
  "fnInfoCallback": function( oSettings, iStart, iEnd, iMax, iTotal, sPre ) {
    return browserInfo;
  }
});

// Hot fix
browser.attr('style', 'width: 100%');
